package lightcar;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.JButton.*;
import javax.swing.plaf.basic.BasicArrowButton;
import javax.swing.event.*;
import javax.swing.JOptionPane;
import java.util.StringTokenizer;

class MyPanel extends JPanel {

    public MyPanel() {
        setBorder(BorderFactory.createLineBorder(Color.black));
        setBackground(Color.WHITE);
    }

    @Override
    public Dimension getPreferredSize() {
        return new Dimension(700,700);
    }

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g);       
        LightCar.world.draw(g);
    }
}

public class MyFrame extends JFrame 
{
    private boolean add_light_mode;
    private int add_car_mode;
    private Point add_car_origin;
    
    public MyFrame()
    {
        super();
        add_light_mode = false;
        add_car_mode = 0;
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        JButton playButton = new JButton("Play");
        playButton.setSize(10, 60);
        JButton pauseButton = new JButton("Pause");
        pauseButton.setSize(10, 60);
        JButton resetButton = new JButton("Reset");
        resetButton.setSize(10, 60);
        JToggleButton addLightButton = new JToggleButton("Add Light");
        addLightButton.setSize(10, 60);
        JButton addCarButton = new JButton("Add Car");

        
        JLabel filler_label1 = new JLabel();
        JLabel filler_label2 = new JLabel();
        JLabel filler_label3 = new JLabel();
        JLabel filler_label4 = new JLabel();
        
        JPanel buttonsPanel = new JPanel();
        buttonsPanel.setLayout(new GridLayout(3,3,10,10)); //second number is columns
        buttonsPanel.setSize(200,200);
        buttonsPanel.add(playButton);
        buttonsPanel.add(pauseButton);
        buttonsPanel.add(resetButton);
        buttonsPanel.add(addLightButton);
        buttonsPanel.add(filler_label1);
        buttonsPanel.add(filler_label2);
        buttonsPanel.add(addCarButton);
        ///////////////////////////////////////////////
        
        JPanel controlPanel = new JPanel();
        controlPanel.setLayout(new FlowLayout(FlowLayout.LEFT));
        controlPanel.setSize(300,300);
        controlPanel.add(buttonsPanel);
        
        MyPanel drawingArea = new MyPanel();
        drawingArea.setLayout(new BorderLayout());
        drawingArea.setSize(700,700);
        add(controlPanel, BorderLayout.EAST);
        add(drawingArea, BorderLayout.WEST);
    
        drawingArea.addMouseListener(new MouseAdapter()
      {
            @Override
            public void mousePressed(MouseEvent e)
            {
                if(add_light_mode)
                {
                    Point loc = new Point(getMousePosition());
                    loc.y -= 24;
                    loc.x -= 2;
                    LightCar.world.addLight(new LightSource(loc));
                }
                else if(add_car_mode == 1)
                {
                    add_car_origin = new Point(getMousePosition());
                    add_car_mode = 2;
                }
                else if(add_car_mode == 2)
                {
                    LightCar.addCar(add_car_origin, new Point(getMousePosition()));
                    add_car_mode = 0;
                }
                LightCar.mainFrame.repaint();
           }
       });
       
     
        playButton.addActionListener(new ActionListener(){

            public void actionPerformed(ActionEvent e){
               if(LightCar.input_debug) System.out.println("Play button was clicked.");
               LightCar.start();
            }
        });
        pauseButton.addActionListener(new ActionListener(){

            public void actionPerformed(ActionEvent e){
               if(LightCar.input_debug) System.out.println("Pause button was clicked.");
               LightCar.pause();
            }
        });
        resetButton.addActionListener(new ActionListener(){

            public void actionPerformed(ActionEvent e){
               if(LightCar.input_debug) System.out.println("Reset button was clicked.");
               LightCar.pause();
               LightCar.world.reset();
               LightCar.mainFrame.repaint();
            }
        });
        addLightButton.addActionListener(new ActionListener(){

              public void actionPerformed(ActionEvent e){
                 System.out.println("light Button was Clicked");             
                 add_light_mode = !add_light_mode;
              }
          });
    
      
        addCarButton.addActionListener(new ActionListener(){

             public void actionPerformed(ActionEvent e){
                 System.out.println("addCar Button was Clicked");  
                 add_car_mode = 1; 
              }
          });
    }
}
